package com.example.cursor_controller;

import com.example.cursor_service.AttributePolicyService;
import com.example.cursor_orm.model.AttributePolicy;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/attribute-policies")
@Tag(name = "属性策略", description = "属性策略相关的API接口")
public class AttributePolicyController {
    private final AttributePolicyService policyService;
    
    @Autowired
    public AttributePolicyController(AttributePolicyService policyService) {
        this.policyService = policyService;
    }
    
    @PostMapping
    @Operation(summary = "创建属性策略", description = "创建新的属性策略")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "策略创建成功"),
        @ApiResponse(responseCode = "400", description = "请求参数错误")
    })
    public AttributePolicy createPolicy(@RequestBody AttributePolicy policy) { 
        return policyService.createPolicy(policy); 
    }
    
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取属性策略", description = "根据策略ID获取属性策略信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取策略成功"),
        @ApiResponse(responseCode = "404", description = "策略不存在")
    })
    public AttributePolicy getPolicyById(@Parameter(description = "策略ID") @PathVariable Integer id) { 
        return policyService.getPolicyById(id); 
    }
    
    @PutMapping("/{id}")
    @Operation(summary = "更新属性策略", description = "根据策略ID更新属性策略信息")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "策略更新成功"),
        @ApiResponse(responseCode = "404", description = "策略不存在")
    })
    public AttributePolicy updatePolicy(@Parameter(description = "策略ID") @PathVariable Integer id, @RequestBody AttributePolicy policy) { 
        return policyService.updatePolicy(id, policy); 
    }
    
    @DeleteMapping("/{id}")
    @Operation(summary = "删除属性策略", description = "根据策略ID删除属性策略")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "策略删除成功"),
        @ApiResponse(responseCode = "404", description = "策略不存在")
    })
    public boolean deletePolicy(@Parameter(description = "策略ID") @PathVariable Integer id) { 
        return policyService.deletePolicy(id); 
    }
    
    @GetMapping
    @Operation(summary = "获取属性策略列表", description = "分页获取属性策略列表")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取策略列表成功")
    })
    public List<AttributePolicy> getPolicyList(@Parameter(description = "页码", example = "1") @RequestParam(defaultValue = "1") int page, 
                                             @Parameter(description = "每页大小", example = "10") @RequestParam(defaultValue = "10") int size) { 
        return policyService.getPolicyList(page, size); 
    }
    
    @GetMapping("/role/{roleId}")
    @Operation(summary = "根据角色获取策略", description = "根据角色ID获取相关的属性策略")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取策略成功")
    })
    public List<AttributePolicy> getPoliciesByRoleId(@Parameter(description = "角色ID") @PathVariable Integer roleId) { 
        return policyService.getPoliciesByRoleId(roleId); 
    }
    
    @GetMapping("/permission/{permissionId}")
    @Operation(summary = "根据权限获取策略", description = "根据权限ID获取相关的属性策略")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取策略成功")
    })
    public List<AttributePolicy> getPoliciesByPermissionId(@Parameter(description = "权限ID") @PathVariable Integer permissionId) { 
        return policyService.getPoliciesByPermissionId(permissionId); 
    }
    
    @GetMapping("/attribute")
    @Operation(summary = "根据属性获取策略", description = "根据属性键值对获取相关的属性策略")
    @ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "获取策略成功")
    })
    public List<AttributePolicy> getPoliciesByAttribute(@Parameter(description = "属性键") @RequestParam String key, 
                                                       @Parameter(description = "属性值") @RequestParam String value) { 
        return policyService.getPoliciesByAttribute(key, value); 
    }
} 